package com.everest.tribalapp;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.everest.tribalapp.Dering_Pengelola_1.Check;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Plik_form1 extends Activity{

	private Button form1;
	private TextView plik_form1_qus1;
	protected int value;
	private TextView plik_form1_qus2;
	private TextView plik_form1_qus3;
	private TextView plik_form1_qus4;
	private TextView plik_form1_qus5;
	private TextView plik_form1_qus6;
	private EditText plik_form1_ans5;
	private EditText plik_form1_ans6;
	private ArrayList<String> stringArrayList1;
	private ArrayList<String> stringArrayList2;
	private ArrayList<String> stringArrayList3;
	private String[] select_district;
	private String[] select_sub;
	private String[] select_desa;
	private ArrayList<String> stringArrayList11;
	private ArrayList<String> stringArrayList12;
	private String[] selectid;
	private String[] selectname;
	private Spinner s;
	private Spinner s1;
	private Spinner s2;
	private Spinner s3;
	@Override
	public void onBackPressed() 
	{
		// TODO Auto-generated method stub
		super.onBackPressed();
		Intent intent=new Intent(getApplicationContext(), Add1.class);
		startActivity(intent);
		Plik_form1.this.finish();
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Plik_form1.this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.plik_form1);
		

		decl();
		qus();
		s=(Spinner)findViewById(R.id.plik_form1_ans1);
		s1=(Spinner)findViewById(R.id.plik_form1_ans2);
		s2=(Spinner)findViewById(R.id.plik_form1_ans3);
		s3=(Spinner)findViewById(R.id.plik_form1_ans4);
		Boolean a;
		 a=isOnline();
		 if(a)
		 {
		 new Check().execute();
		 }
		 else
		 {
			 
				ArrayAdapter adapter = new ArrayAdapter(Plik_form1.this,android.R.layout.simple_spinner_item, Global.province);
				s.setAdapter(adapter);
				ArrayAdapter adapter1 = new ArrayAdapter(Plik_form1.this,android.R.layout.simple_spinner_item, Global.district);
				s1.setAdapter(adapter1);
				ArrayAdapter adapter2 = new ArrayAdapter(Plik_form1.this,android.R.layout.simple_spinner_item, Global.subdistrict);
				s2.setAdapter(adapter2);
				ArrayAdapter adapter3 = new ArrayAdapter(Plik_form1.this,android.R.layout.simple_spinner_item, Global.desa);
				s3.setAdapter(adapter3);
				
		 }
		
		
		form1=(Button)findViewById(R.id.plik_form1_b1);
		
		form1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent in=new Intent(Plik_form1.this,Plik_form2.class);
				startActivity(in);
				Plik_form1.this.finish();
			}
		});
	}
	
	void decl()
	{
		plik_form1_qus1=(TextView)findViewById(R.id.plik_form1_qus1);
		plik_form1_qus2=(TextView)findViewById(R.id.plik_form1_qus2);
		plik_form1_qus3=(TextView)findViewById(R.id.plik_form1_qus3);
		plik_form1_qus4=(TextView)findViewById(R.id.plik_form1_qus4);
		plik_form1_qus5=(TextView)findViewById(R.id.plik_form1_qus5);
		plik_form1_qus6=(TextView)findViewById(R.id.plik_form1_qus6);
		plik_form1_ans5=(EditText)findViewById(R.id.plik_form1_ans5);
		plik_form1_ans6=(EditText)findViewById(R.id.plik_form1_ans6);
		
	}
	void qus()
	{
		plik_form1_qus1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
			value=R.string.plik_form1_qus1;
			toast_msg();
				
			}
		});
plik_form1_qus2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
			value=R.string.plik_form1_qus2;
			toast_msg();
				
			}
		});
plik_form1_qus3.setOnClickListener(new View.OnClickListener() {
	
	@Override
	public void onClick(View arg0) {
	value=R.string.plik_form1_qus3;
	toast_msg();
		
	}
});
plik_form1_qus4.setOnClickListener(new View.OnClickListener() {
	
	@Override
	public void onClick(View arg0) {
	value=R.string.plik_form1_qus4;
	toast_msg();
		
	}
});
plik_form1_qus5.setOnClickListener(new View.OnClickListener() {
	
	@Override
	public void onClick(View arg0) {
	value=R.string.plik_form1_qus5;
	toast_msg();
		
	}
});
plik_form1_qus6.setOnClickListener(new View.OnClickListener() {
	
	@Override
	public void onClick(View arg0) {
	value=R.string.plik_form1_qus6;
	toast_msg();
		
	}
});
	}
	
	public void toast_msg()
	{
		LayoutInflater inflater = getLayoutInflater();

		View layout = inflater.inflate(R.layout.custom_toast,
				(ViewGroup) findViewById(R.id.custom_toast_layout_id));
		TextView text = (TextView) layout.findViewById(R.id.text);
		text.setText(value);
		Toast toast = new Toast(getApplicationContext());
		toast.setGravity(Gravity.BOTTOM, 0, 0);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.setView(layout);
		toast.show();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		SharedPreferences preferences=getSharedPreferences("plik_f1_details",MODE_PRIVATE);
		SharedPreferences.Editor editor=preferences.edit();
		editor.putString("1_6",plik_form1_ans6.getText().toString().trim());
		editor.putString("1_5",plik_form1_ans5.getText().toString().trim());
		editor.putString("1_1",s.getSelectedItem().toString().trim());
		editor.putString("1_2",s1.getSelectedItem().toString().trim());
		editor.putString("1_3",s2.getSelectedItem().toString().trim());
		editor.putString("1_4",s3.getSelectedItem().toString().trim());
//		editor.putString("1_7",dering_f1_ans_7.getText().toString().trim());
		editor.commit();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		SharedPreferences preferences=getSharedPreferences("plik_f1_details",MODE_PRIVATE);
		plik_form1_ans5.setText(preferences.getString("1_5",""));
		plik_form1_ans6.setText(preferences.getString("1_6",""));
//		dering_f1_ans_7.setText(preferences.getString("1_7",""));
	}
	
	
	protected void province()
	{
		   boolean answer;
	        answer=isOnline();
	        System.out.println(answer);
          	stringArrayList11 = new ArrayList<String>();
          	stringArrayList12 = new ArrayList<String>();
	        
	        if(answer)
	        {
	                 
	        JSONObject json = JSONfunctions.getJSONfromURL(Global.province_url);           
		        try{
	        	JSONObject response1=json.getJSONObject("response");
	          	response1.toString();
	        	JSONArray  data = response1.getJSONArray("items");
	        	
	        
	            for(int i=0;i<data.length();i++)
	            {						
	    			
	    			JSONObject e = data.getJSONObject(i);
	    			     stringArrayList11.add(e.getString("id"));
	    			     stringArrayList12.add(e.getString("name"));
	    			     	
	    			     
	            	
	            }	
	            selectid = stringArrayList11.toArray(new String[stringArrayList11.size()]);
	            selectname = stringArrayList12.toArray(new String[stringArrayList12.size()]);
	            System.out.println(selectid[0]+selectname[0]);
	        }catch(JSONException e)        
	        {
	        	System.out.println(e);
	        	 Log.e("log_tag", "Error parsing data "+e.toString());
	        }
	        
	       
	       
	    }
	    else
	    {

	    	Toast.makeText(getApplicationContext(), "No Internet Connection",Toast.LENGTH_LONG).show();
	    } 
		
	}
	protected void detail()
	{
		   boolean answer;
	        answer=isOnline();
	        System.out.println(answer);
          	stringArrayList1 = new ArrayList<String>();
          	stringArrayList2 = new ArrayList<String>();
          	stringArrayList3 = new ArrayList<String>();
	        
	        if(answer)
	        {
	                 
	        JSONObject json = JSONfunctions.getJSONfromURL(Global.detail_url);           
		        try{
	        	JSONObject response1=json.getJSONObject("response");
	          	response1.toString();
	        	JSONArray  data = response1.getJSONArray("items");
	        	
	        
	            for(int i=0;i<data.length();i++)
	            {						
	    			
	    			JSONObject e = data.getJSONObject(i);
	    			     stringArrayList1.add(e.getString("district"));
	    			     stringArrayList2.add(e.getString("sub_district"));
	    			     stringArrayList3.add(e.getString("desa"));
	    			     	
	    			     
	            	
	            }	
	            select_district = stringArrayList1.toArray(new String[stringArrayList1.size()]);
	            select_sub = stringArrayList2.toArray(new String[stringArrayList2.size()]);
	            select_desa = stringArrayList3.toArray(new String[stringArrayList3.size()]);
	        }catch(JSONException e)        
	        {
	        	System.out.println(e);
	        	 Log.e("log_tag", "Error parsing data "+e.toString());
	        }
	        
	       
	       
	    }
	    else
	    {

	    	Toast.makeText(getApplicationContext(), "No Internet Connection",Toast.LENGTH_LONG).show();
	    } 
		
	}
	public boolean isOnline() 
	{
	    ConnectivityManager cm =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo netInfo = cm.getActiveNetworkInfo();
	    if (netInfo != null && netInfo.isConnectedOrConnecting())
	    {
	        return true;
	    }
	    return false;
	}
	
	
	class Check extends AsyncTask{
		//
				
				private String json;
				private JSONObject jsonobject;
				private String code;
				private String msg;
				private String url;
				private Dialog dialog;
				@Override
				protected void onPreExecute() 
				{
					super.onPreExecute();
					dialog=new Dialog(Plik_form1.this);
					dialog.setTitle("Loading...");
					dialog.setCancelable(false);
					dialog.show();
					
				}

				@Override
				protected Object doInBackground(Object... params) 
				{
					
					try 
					{
						province();
						detail();
					} 
					catch (Exception e) 
					{
						e.printStackTrace();
					}
					return null;
				}
				@Override
				protected void onPostExecute(Object result)
				{
					
					if(selectname.length>0)
					{
					ArrayAdapter adapter = new ArrayAdapter(Plik_form1.this,android.R.layout.simple_spinner_item, selectname);
					s.setAdapter(adapter);
					}
					if(select_district.length>0)
					{
						ArrayAdapter adapter1 = new ArrayAdapter(Plik_form1.this,android.R.layout.simple_spinner_item, select_district);
						s1.setAdapter(adapter1);
					}
					if(select_sub.length>0)
					{
						ArrayAdapter adapter2 = new ArrayAdapter(Plik_form1.this,android.R.layout.simple_spinner_item, select_sub);
						s2.setAdapter(adapter2);
					}
					if(select_desa.length>0)
					{
						ArrayAdapter adapter3 = new ArrayAdapter(Plik_form1.this,android.R.layout.simple_spinner_item, select_desa);
						s3.setAdapter(adapter3);
					}
					
					dialog.dismiss();
					
				}

			}
}
